package com.winding.plugin.swagger.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * <p>TODO
 *
 * @author wind
 * @version 1.0
 * @since 2018/12/5
 */
@Slf4j
public class JsonUtils {
    public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    public static final ObjectMapper OBJECT_MAPPER_ALL = new ObjectMapper();

    static {
        OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        OBJECT_MAPPER_ALL.setSerializationInclusion(JsonInclude.Include.ALWAYS);
    }

    public static String toJson(Object object) {
        if (object != null) {
            try {
                return OBJECT_MAPPER.writeValueAsString(object);
            } catch (JsonProcessingException e) {
                log.warn("JsonProcessingException", e);
            }
        }
        return StringUtils.EMPTY;
    }

    public static String toJsonPretty(Object object) {
        if (object != null) {
            try {
                return OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(object);
            } catch (JsonProcessingException e) {
                log.warn("JsonProcessingException", e);
            }
        }
        return StringUtils.EMPTY;
    }

    public static String toNullJson(Object object) {
        if (object != null) {
            try {
                return OBJECT_MAPPER_ALL.writeValueAsString(object);
            } catch (JsonProcessingException e) {
                log.warn("JsonProcessingException", e);
            }
        }
        return StringUtils.EMPTY;
    }

}
